<?php
/**
 * 保养记录
 * @Author    紫梦
 * @DateTime  2018-01-10
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\Maintain as MaintainModel;
use app\common\model\Cars;
use app\common\request\Oss;

class Maintain extends Base
{
	
	/**
     * 保养列表
     */
    public function index()
    {
        $model = MaintainModel::All();
        $this->assign('data',$model);
        return $this->fetch();
    }

    /**
     * 删除保养记录
     */
    public function delete($id)
    {
        $msg['status'] = 0;
        $res = MaintainModel::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 编辑保养记录
     */
    public function edit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $model = new MaintainModel();
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }
            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('maintain_file')->insert(['maintain_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            }
            $msg['msg'] = '更新成功';
            return $msg;

        }else{

            $model = MaintainModel::get($id);
            $this->assign('data',$model);
            return $this->fetch();

        }

    }

    /**
     * 添加保养记录
     */
    public function add()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();          
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车牌不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_unique_id'] = $car['car_unique_id'];
            
            $model = new MaintainModel();                 
            $model->allowField(true)->save($data);

            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('maintain_file')->insert(['maintain_id'=>$model->id,'file_url'=>$url]);
                    }
                }

            }   
            if($model->id){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }
            return $msg;
           
        }else{

            
            return $this->fetch();

        }
    }
    /**
     * 删除租用记录附件
     */
    public function del_file($id)
    {
        $msg['status'] = 0;
        $url = Db::name('maintain_file')->where(['id'=>$id])->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){
            $res = Db::name('maintain_file')->delete($id);
            
        }
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }
}